package com.thug.entities;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.thug.engine.BaseGameEntity;
import com.thug.engine.Utility;

public class Sounder extends BaseGameEntity {

	private AnimatedSprite mSprite;
	private Body mBody;
	private Body mSoundConeSensor;

	private float mCurrentDuration;
	private final float mStartSoundingTime;// = 1000;

	private boolean mPersonToched;
	private boolean mStartSounding;

	public Sounder(AnimatedSprite sounderSprite, Body sounderBody, Body soundConeSensor) {
		mEntityType = EntityType.ET_SOUNDER;
		mSprite = sounderSprite;
		mBody = sounderBody;
		mSoundConeSensor = soundConeSensor;
		mStartSoundingTime = 10;
		disableSounder();
	}

	@Override
	public void init() {
		setActive(false);		
	}

	@Override
	public void attach(Scene scene) {
		scene.attachChild(mSprite);
	}

	@Override
	public void detach(Scene scene) {
		scene.detachChild(mSprite);
	}

	@Override
	public void update(float elapsedTime) {
		mCurrentDuration += elapsedTime;
		if ((mCurrentDuration > mStartSoundingTime) && (mStartSounding == false)){
			startSounding();
		}

		//
		if(mPersonToched) {
			disableSounder();
		}


		if(mBody.isActive()) {
			Utility.updateSprite(mSprite, mBody, true, true);
		}
	}


	@Override
	public void beginContact(Body bodyA, Body bodyB, Vector2 normal,
			Vector2[] points) {
		if(mStartSounding) {
			BaseGameEntity aEntity = (BaseGameEntity) bodyA.getUserData();
			BaseGameEntity bEntity = (BaseGameEntity) bodyB.getUserData();
			if((aEntity.getType() == EntityType.ET_PLAYER) || (bEntity.getType() == EntityType.ET_PLAYER)){
				if((bodyA == mBody) || (bodyB == mBody)) {
					mPersonToched = true;
				}
			}
		}

	}

	@Override
	public void endContact(Body bodyA, Body bodyB, Vector2 normal,
			Vector2[] points) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		mSprite.detachSelf();
		mBody.getWorld().destroyBody(mBody);
		mSoundConeSensor.getWorld().destroyBody(mSoundConeSensor);
		mSprite = null;
		mBody = null;
		mSoundConeSensor = null;
	}


	public void throwSounder(Vector2 position, Vector2 direction, float power) {
		setActive(false);
		mBody.setActive(true);
		mSoundConeSensor.setActive(false);
		mSprite.setVisible(true);

		mBody.setTransform(position, mBody.getAngle());
		mBody.applyLinearImpulse(direction.mul(power), mBody.getWorldCenter());
		mCurrentDuration = 0;
		mStartSounding = false;

	}

	private void startSounding() {
		mStartSounding = true;
		setActive(true);
		mBody.setLinearVelocity(new Vector2(0, 0));
		mSoundConeSensor.setTransform(mBody.getPosition(), mBody.getAngle());
		mSoundConeSensor.setActive(true);
		//sprite animate
		mSprite.setColor(1, 0, 0);
	}

	private void disableSounder() {
		mPersonToched = false;
		mBody.setActive(false);
		mSoundConeSensor.setActive(false);
		mSprite.setVisible(false);
		mStartSounding = false;
		setActive(false);
	}

	@Override
	public float getRotation() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public float getX() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public float getY() {
		// TODO Auto-generated method stub
		return 0;
	}


}
